{% extends "index.html" %}

{% block content %}

	<div class="row">
		<div class="col-xs-12">
											
										
			<h3 class="header smaller lighter blue">
				<i class="icon-hand-right icon-animated-hand-pointer blue"></i>
				实例模板列表
			</h3>
			<div class="col-sm-3">
										<div class="widget-box">
											<div class="widget-header widget-header-flat">
												<h4>Tips</h4>
											</div>

											<div class="widget-body">
												<div class="widget-main">
													<ul class="list-unstyled spaced2">
														<li>
															<i class="icon-circle green"></i>
															该功能用于快速创建虚拟机
														</li>
																			
							
															<li class="muted">
																<i class="icon-warning-sign orange"></i>
																模板注意事项：
																								
															</li>
						
															<li>
																<ul class="list-unstyled">
																	<li>
																		<i class="icon-caret-right blue"></i>
																		默认不会创建网络接口，需要稍后自行添加
																	</li>
						
																	<li>
																		<i class="icon-caret-right blue"></i>
																		模板名字不能相同
																	</li>
						
																</ul>
															</li>																							
													</ul>													
												</div>
											</div>
										</div>
									</div>
									
									
					<div class="col-sm-6">
											
									
										<div class="tabbable">
											<ul class="nav nav-tabs padding-12 tab-color-blue background-blue" id="myTab4">
												<li class="active">
													<a data-toggle="tab" href="#profile4">模板列表</a>
												</li>											
												


											</ul>

											<div class="tab-content">
												

												
												<div id="profile4" class="tab-pane in active">
													<div class="widget-toolbox padding-8 clearfix">
		
														<button class="btn btn-xs btn-success pull-left" class="btn btn-primary btn-lg" data-toggle="modal"  data-target="#addTempModal">
															<i class="icon-arrow-right icon-plus"></i>
															<span class="bigger-110">添加模板</span>
														</button>
													</div>												
													<div>
														<table class="table table-striped table-bordered">
															<thead>
																<tr>
																	<th class="center">序号</th>
																	<th class="center">模板名称</th>
																	<th class="hidden-xs center">CPU</th>
																	<th class="hidden-480 center">内存(MB)</th>
																	<th class="center">系统盘(GB)</th>
																	<th class="center">操作</th>
																</tr>
															</thead>
										
															<tbody>
																{% for ds in tempList %}
																<tr>
																	<td class="center">{{ forloop.counter }}</td>
																	<td class="center">
																		<a>{{ ds.name }}</a>
																	</td>
																		<td class="hidden-xs center">
																			{{ ds.cpu }}									
																	</td>
																	<td class="center">
																			{{ ds.mem }}																		
																	</td>
																	<td class="center">
																		{{ ds.disk }}											
																	</td>
																	<td class="center">
																		
																		<button class="btn btn-xs btn-danger" onclick="onBtnHandleTemp(this,'{{ds.name}}','delete')">
																			<i class="icon-trash  bigger-110 icon-only"></i>
																		</button>											
																	</td>
																</tr>
																{% endfor %}
															</tbody>
														</table>
													</div>													
													<div class="hr "></div>
										
													<div class="row">
														<div class="col-sm-5 pull-right">
															<h4 class="pull-right">
																Total Template :
																<span class="red">{{ tempList|length }}</span>
															</h4>
														</div>
														<div class="col-sm-7 pull-left"> 汇总信息 </div>
													</div>													
												</div>

											</div>
										</div>
									</div>				
			
		</div>
		
		
		<!-- 模态框（Modal） -->
		<div class="modal fade" id="addTempModal" tabindex="-1" role="dialog" aria-labelledby="addTempModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title" id="addTempModalLabel">
							添加实例模板
						</h4>
					</div>
					<div class="modal-body">
						
						<form class="form-horizontal" id="addTemp">
														
								<div class="form-group">
										<label class="col-sm-3 control-label no-padding-right" for="form-field-4"><strong>模板名称</strong></label>
							
											<div class="col-sm-9">
											<input class="input-sm" type="text" id="form-field-6" placeholder="" name="name" />																				
										</div>
								</div>

								<div class="form-group">
										<label class="col-sm-3 control-label no-padding-right" for="form-field-4"><strong>CPU</strong></label>
							
											<div class="col-sm-9">
											<input class="input-sm" type="text" id="form-field-6" placeholder="" name="cpu" />																				
										</div>
								</div>	
								<div class="form-group">
										<label class="col-sm-3 control-label no-padding-right" for="form-field-4"><strong>内存(MB)</strong></label>
							
											<div class="col-sm-9">
											<input class="input-sm" type="text" id="form-field-6" placeholder="" name="mem" />																				
										</div>
								</div>	
								<div class="form-group">
										<label class="col-sm-3 control-label no-padding-right" for="form-field-4"><strong>系统盘(GB)</strong></label>
							
											<div class="col-sm-9">
											<input class="input-sm" type="text" id="form-field-6" placeholder="" name="disk" />																				
										</div>
								</div>							
																
																		
						</form>	
						
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭
						</button>
						<button type="button" class="btn btn-primary" onclick="addTemp(this,'add')">
							添加
						</button>
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>
		
		
	</div>
	<script type="text/javascript">
			function addTemp(obj,op) {	
				var btnObj = $(obj);
				btnObj.attr('disabled',true);
					var server_data = {};
					var required = ['name','cpu',"mem","disk"]; /* 必填项列表 */
					var form = document.getElementById('addTemp');
					for (var i = 0; i < form.length; ++i) {
						var name = form[i].name;
						var value = form[i].value;	
						idx = $.inArray(name, required);						
						if (idx >= 0 && value.length == 0){
							window.wxc.xcConfirm("请注意必填项不能为空~", window.wxc.xcConfirm.typeEnum.error);
							btnObj.removeAttr('disabled');
							return false;
						};	
						if (  value.length != 0 && name.length != 0 ){
							server_data[name] = value;
						};					
					};				
					server_data['op'] = op
					$.ajax({
						dataType: "JSON",
						url:'/tempInstance/', //请求地址
						type:"POST",  //提交类似
						data:server_data,  //提交参数
						success:function(response){
							btnObj.removeAttr('disabled');
							if (response["code"] == 200){
								window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);
								location.reload();
							}
							else {
								window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
							}
							
						},
				    	error:function(response){
				    		btnObj.removeAttr('disabled');
				    		window.wxc.xcConfirm(response["data"], window.wxc.xcConfirm.typeEnum.error);
				    	}
					});	
				}
	</script>
{% endblock %}